#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <strings.h>

#include "button.h"

int main(void)
{
	int fd;
	int ret;
	struct BUTTON button;
	
	/*打开设备文件*/
	fd = open("/dev/button",O_RDWR|O_NONBLOCK);//非阻塞打开
	if(fd < 0)
	{
		perror("open");
		return 1;
	}

	/*读写设备文件*/
	while(1)
	{
		bzero(&button, sizeof(button));
		ret = read(fd, &button, sizeof(button));
		//if(ret <= 0)	continue;

		if (button.code == 'A')
		{
			if (button.value == 1) 
				printf("power key pressed.\n");
			else
				printf("power key released.\n");
		}

		sleep(1);
	}

	/*关闭设备文件*/	
	close(fd);

	return 0;
}
